package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.SlidingSmallBean;
import cn.com.ddapp.ddm.util.TDevice;


public class SlidingSmallAdpter extends BaseAdapter {
    private Context mContext;
    private List<SlidingSmallBean.brandList> mLists ;
    private LayoutInflater layoutInflater;

    public SlidingSmallAdpter(Context context, List<SlidingSmallBean.brandList> lists) {
        mContext = context;
        mLists = lists;
        layoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mLists != null ? mLists.size():0;
    }

    @Override
    public Object getItem(int i) {
        return mLists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void addView(List<SlidingSmallBean.brandList> ll){
        if(ll.size()<=0){
            return;
        }else{
            mLists.addAll(ll);
        }
    }

    public void checkAll(boolean checked) {
        for (int i = 0 ; i < mLists.size(); i++) {
            SlidingSmallBean.brandList beana = mLists.get(i);
            beana.rb_select_cart=checked;
        }
        notifyDataSetChanged(); // 刷新列表
    }

    public void clear(){
        for (int i = 0; i < mLists.size(); i++) {
            mLists.get(i).setCheck(false);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(convertView == null){
            convertView= layoutInflater.inflate(R.layout.sliding_new_item,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.auto(convertView);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
//        int width=(int) (TDevice.getScreenWidth()-100)/3;
//        int height=(int) (TDevice.getScreenWidth()-100)/15;
//        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width,height);
//        holder.slidingbig_name.setLayoutParams(params);
        holder.slidingbig_name.setId(mLists.get(position).getId());
        holder.slidingbig_name.setTag(mLists.get(position).getId());
        holder.slidingbig_name.setText(mLists.get(position).getName());
        holder.slidingbig_name.setChecked(mLists.get(position).isCheck());
        holder.slidingbig_name.setOnClickListener(seletListener);
        holder.slidingbig_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                String tag = (String)(compoundButton.findViewById(compoundButton.getId()).getTag()+"");
            }
        });
        return convertView;
    }
  public View.OnClickListener seletListener= new View.OnClickListener() {
      @Override
      public void onClick(View view) {
//            String value=mLists.get()
      }
  };

    static class ViewHolder{
        @BindView(R.id.slidingbig_name)
        CheckBox slidingbig_name;


        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
